//
//  SettingTableViewController.m
//  caiPiao
//
//  Created by 梅杰 on 16/8/6.
//  Copyright © 2016年 梅杰. All rights reserved.
//

#import "SettingTableViewController.h"
#import "SettingArrowItem.h"
#import "SettingSwitchItem.h"
#import "SettingGroup.h"
#import "SettingCell.h"
#import "MBProgressHUD+BWMExtension.h"

@interface SettingTableViewController ()
@property (nonatomic,strong) NSMutableArray *data;
@end

@implementation SettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)data{
    if (_data == nil) {
        _data = [NSMutableArray array];
        
        SettingItem *item00 = [SettingArrowItem itemWithIcon:@"MorePush" title:@"推送和提醒" vcClass:nil];
        SettingItem *item01 = [SettingArrowItem itemWithIcon:@"handShake" title:@"清除缓存"];
        
        SettingGroup *group1 = [[SettingGroup alloc] init];

        group1.items = @[item00,item01];
        
        SettingItem *item10 = [SettingArrowItem itemWithIcon:@"MoreUpdate" title:@"检查新版本"];

        SettingItem *item11 = [SettingArrowItem itemWithIcon:@"MoreHelp" title:@"帮助" vcClass:nil];
        
        item01.option = ^(){
            NSString *cachePath = CachePath;
            NSString *thumbPath = [cachePath stringByAppendingPathComponent:@"ThumbImage"];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSArray *contents = [fileManager contentsOfDirectoryAtPath:thumbPath error:nil];
            
            NSString *extension = @"png";
            MBProgressHUD *deleHud = [MBProgressHUD bwm_showHUDAddedTo:self.view title:@"正在删除" animated:YES];
            for (NSString *fileName in contents) {
                if ([[fileName pathExtension] isEqualToString:extension]) {
                    NSString *delePath = [thumbPath stringByAppendingPathComponent:fileName];
                    [fileManager removeItemAtPath:delePath error:nil];
                }
                
            }
//            [MBProgressHUD bwm_showHUDAddedTo:self.view title:@"正在删除" animated:YES];
            [deleHud bwm_hideWithTitle:@"清除完成" hideAfter:1.0 msgType:BWMMBProgressHUDMsgTypeSuccessful];
        };
        
        SettingGroup *group2 = [[SettingGroup alloc] init];
//        group2.header = @"测试二";
//        group2.footer = @"测试二";
        group2.items = @[item10,item11];
        
        [_data addObject:group1];
        [_data addObject:group2];
    }
    return _data;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    SettingGroup *group = self.data[section];
    return group.items.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    SettingGroup *group = self.data[section];
    return group.header;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    SettingGroup *group = self.data[section];
    return group.footer;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SettingCell *cell = [SettingCell cellWithTableView:tableView];
    
    SettingGroup *group = self.data[indexPath.section];
    SettingItem *item = group.items[indexPath.row];
    cell.item = item;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SettingGroup *group = self.data[indexPath.section];
    SettingItem *item = group.items[indexPath.row];
    
    if (item.option){
        item.option();
    }
    
    if ([item isKindOfClass:[SettingArrowItem class]]) {
        SettingArrowItem *arrwoItem = (SettingArrowItem *)item;
        if (arrwoItem.vcClass == nil) {
            return;
        }
        
        UIViewController *vc = [[arrwoItem.vcClass alloc] init];
        vc.view.backgroundColor = [UIColor whiteColor];
        vc.title = item.title;
        [self.navigationController pushViewController:vc animated:YES];
    }

}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
